#!/usr/bin/env bash
set -e

LOG_FILE="/root/result.log"
cur_path=$(pwd)

echo "start to prepare ut env"
chmod +x /root/repo/ci-repos/.common_scripts/common.sh
source /root/repo/ci-repos/.common_scripts/common.sh
set_gitee_hosts
prepare_git $1 $2
prepare_conan2 $3 $4
prepare_bingo
prepare_sdk

echo "start to ut.........."
cd ${cur_path}
bingo test -ut -jit | tee ${LOG_FILE} 2>&1 || exit 1
CNT=$(grep -c -e ' [0-9]\+ failure' -e ' [0-9]\+ error' ${LOG_FILE} ||:)
if [ ${CNT} -eq 0 ];then
  CPASSCNT=$(grep -c "PASS: " ${LOG_FILE} ||:)
  CFAILCNT=$(grep -c "FAIL: " ${LOG_FILE} ||:)
  if [ ${CPASSCNT} -ge 0 ] && [ ${CFAILCNT} -eq 0 ]; then
    echo "[INFO] c lang unit test success"
    exit 0
  else
    echo "[ERROR] ERROR: unit test failed, no result found or c lang test fail!======================"
    exit 1
  fi
fi

FAIL_CNT=$(grep -e ' [0-9]\+ failure' -e ' [0-9]\+ error' ${LOG_FILE} | head -1 | awk '{print $10}') ||:
[ -n "${FAIL_CNT}" ] || FAIL_CNT=0
if [ ${FAIL_CNT} -ne 0 ]; then
  echo "[ERROR] ERROR: unit test failed/error, $FAIL_CNT test case failed/error!====="
  exit 1
fi